//201902104003邵潇漩
package bysj.controller.basic;

import bysj.domain.ApplicationForSupervisor;
import bysj.domain.ApplicationForSupervisorEntry;
import bysj.service.ApplicationForSupervisorService;
import bysj.service.StudentService;
import bysj.service.TeacherService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import util.JSONUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;

@Slf4j
@WebServlet("/applicationForSupervisor.ctl")
public class ApplicationForSupervisorController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //设置请求字符编码为UTF-8
        //request.setCharacterEncoding("UTF-8");
        //根据request对象，获得代表参数的JSON字串
        String applicationForSupervisor_json = JSONUtil.getJSON(request);
        JSONObject req_jsonObj = JSON.parseObject(applicationForSupervisor_json);
        //将JSON字串解析为Degree对象
        ApplicationForSupervisor applicationForSupervisorToAdd = JSON.parseObject(applicationForSupervisor_json, ApplicationForSupervisor.class);
        //设置响应字符编码为UTF-8
        //response.setContentType("application/json;charset=UTF-8");
        //创建JSON对象message，以便往前端响应信息
        JSONObject message = new JSONObject();
        //在数据库表中增加Degree对象
        try {
            Object studentId_obj = req_jsonObj.get("student_id");
            int studentId_int = Integer.parseInt(studentId_obj.toString());
            applicationForSupervisorToAdd.setStudent(StudentService.getInstance().find(studentId_int));
            JSONArray applicationForSupervisorEntries_json = req_jsonObj.getJSONArray("applicationForSupervisorEntries");
            Collection<ApplicationForSupervisorEntry> applicationForSupervisorEntries=applicationForSupervisorEntries_json.toJavaList(ApplicationForSupervisorEntry.class);
            for(ApplicationForSupervisorEntry a:applicationForSupervisorEntries){
                Object teacherId_obj = req_jsonObj.get("teacher_id");
                //a.setTeacher(TeacherService.getInstance().find(a.getTeacher_id()));
                a.setTeacher(TeacherService.getInstance().find(Integer.parseInt(teacherId_obj.toString())));
            }
            applicationForSupervisorToAdd.setApplicationForSupervisorEntries(applicationForSupervisorEntries);
            ApplicationForSupervisorService.getInstance().add(applicationForSupervisorEntries,applicationForSupervisorToAdd);
            message.put("message", "增加成功");
        }catch (SQLException e){
            message.put("message", "数据库操作异常");
            log.error("数据库操作异常:"+e);
        }catch(Exception e){
            message.put("message", "网络异常");
            log.error("网络异常:"+e);
        }
        //响应message到前端
        response.getWriter().println(message);
    }
}
